package com.epoint.clubmanagement.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Title: MyMetaObjectHandler
 * Description: 字段填充类
 *
 * @author sqn
 * @version 1.0.0
 * @date 2021/3/19 0019 下午 9:53
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Date nowTime = new Date();
        log.info("start insert fill ...." + nowTime);
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getSession().getAttribute("username");
        this.setFieldValByName("founder", username, metaObject);
        this.setFieldValByName("modifier", username, metaObject);
        this.setFieldValByName("updateTime", nowTime, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Date nowTime = new Date();
        log.info("start update fill ...." + nowTime);
        this.setFieldValByName("updateTime", nowTime, metaObject);
        Subject subject = SecurityUtils.getSubject();
        String modifier = (String) subject.getSession().getAttribute("username");
        this.setFieldValByName("modifier", modifier, metaObject);

    }
}
